package com.yhj.wchat.controller;

import com.wf.captcha.ArithmeticCaptcha;
import com.yhj.wchat.entity.dto.SendAddFriendDto;
import com.yhj.wchat.entity.dto.TokenUserInfoDto;
import com.yhj.wchat.entity.po.UserContact;
import com.yhj.wchat.entity.po.UserInfo;
import com.yhj.wchat.entity.vo.*;
import com.yhj.wchat.exception.BusinessException;
import com.yhj.wchat.mapper.QunInfoMapper;
import com.yhj.wchat.mapper.UserContactNewMapper;
import com.yhj.wchat.service.QunInfoService;
import com.yhj.wchat.service.UserContactNewService;
import com.yhj.wchat.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/contact")
@Slf4j

public class ContactController extends ABaseController {
    @Resource
    private UserContactNewService userContactNewService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private QunInfoService qunInfoService;
    @PostMapping("/loadContactUsers")
    public ResponseVO loadContactUsers() {
        // 获取请求体参数
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo();
        if (tokenUserInfo == null) {
            throw  new BusinessException("token无效");
        }
        String userId = tokenUserInfo.getUserId();
        //String userId = "282957604653";
        List<ContactUserVO> contactUserVOS = null;
        List<ContactQunVO> qunVOS = null;
        List<String> userIds =  userContactNewService.getContactUsers(userId,1);
        if (userIds == null || userIds.size() == 0) {
            contactUserVOS = Collections.emptyList();
        }
        else {
            contactUserVOS =userInfoService.getUserContact(userIds);
        }
        List<String> qunIds =  userContactNewService.getContactUsers(userId,2);
        if (qunIds == null || qunIds.size() == 0) {
            qunVOS = Collections.emptyList();
        }
        else {
            qunVOS =  qunInfoService.getQunInfoByQunIds(qunIds);
        }
       // List<String> qunIds = userContactNewService.getContactQun(userIds);

        ContactUsersVO  contactUsersVO = new ContactUsersVO();
        contactUsersVO.setContactUsers(contactUserVOS);
        contactUsersVO.setContactQunVOS(qunVOS);
        return getSuccessResponseVO(contactUsersVO);
    }

    /***
     * 搜索好友
     * @param searchKeyWord
     * @return
     */
    @PostMapping("/searchFriends")
    public ResponseVO searchFriends(@RequestParam String searchKeyWord) {
        // 获取请求体参数
       List<SearchFriendVO> users =userInfoService.searchFriends(searchKeyWord);

        return getSuccessResponseVO(users);
    }

    /**
     * 申请添加好友
     * @param sendAddFriendDto
     * @return
     */
    @PostMapping("/sendAddFriend")
    public ResponseVO sendAddFriend(@RequestBody SendAddFriendDto sendAddFriendDto) {
        try{
            String content = sendAddFriendDto.getContent();
            String receiverId = sendAddFriendDto.getReceiverId();
            TokenUserInfoDto tokenUserInfo = getTokenUserInfo();
            String userId = tokenUserInfo.getUserId();
            // 获取请求体参数
            Boolean flag = userInfoService.sendAddFriend(userId,receiverId,content);
            if (flag){
                return getSuccessResponseVO("发送成功");
            }else {
                return getSuccessResponseVO("发送失败");
            }
        }
        catch (BusinessException e){
            return getBusinessErrorResponseVO(e,null);
        }
    }

    /**
     * 获取好友申请列表
     * @return
     */
    @PostMapping("/getApplyFriend")
    public ResponseVO getApplyFriend() {
        try{
            TokenUserInfoDto tokenUserInfo = getTokenUserInfo();
            String userId = tokenUserInfo.getUserId();
         //   String userId = "282957604653";
            // 获取请求体参数
            List<UserContactApplyNewMessageVO> users = userInfoService.getApplyFriend(userId);
            return getSuccessResponseVO(users);
        }
        catch (BusinessException e){
            return getBusinessErrorResponseVO(e,null);
        }
    }

}
